/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.inventory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import model.inventory.Unit;


/**
 *
 * @author Yusup Jauhari Shandi <in.visi>
 */
public class UnitTM extends AbstractTableModel{
    
    private List<Unit> data;

    public UnitTM() {
       this(new ArrayList<Unit>(0));
    }

    public UnitTM(List<Unit> data) {
        this.data = data;
    }

    public List<Unit> getData() {
        return data;
    }
    
     public Unit get(int index) {
        return data.get(index);
    }
     
     public void add(Unit product) {
        data.add(product);
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
    }

    public void remove(int index) {
        data.remove(index);
        fireTableRowsDeleted(index, index);
    }

    public void set(int index, Unit product) {
        data.set(index, product);
        fireTableRowsUpdated(index, index);
    }

    public void clear() {
        data.clear();
        fireTableDataChanged();
    }

    public void add(Collection<Unit> products) {
        data.addAll(products);
        fireTableRowsInserted(getRowCount() - (products.size()), getRowCount() - 1);
    }
    
    @Override
    public int getRowCount() {
       return this.data.size();
    }

    @Override
    public int getColumnCount() {
        return 4;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
         switch (columnIndex) {
            case 0: return rowIndex+1;
            case 1: return data.get(rowIndex).getKode();
            case 2: return data.get(rowIndex).getNama();
            case 3: return data.get(rowIndex).getId();
            default : return null;    
           
         }
    }
    
    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0: return "NO";
            case 1: return "KODE";
            case 2: return "NAMA";
            
            case 3: return "ID";
            default: return null;
        }
    }
    
}
